<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Qingwuit\Models\Address;
use App\Qingwuit\Models\Parlours;
use App\Qingwuit\Models\Riders;
use App\Qingwuit\Models\Store;
use App\Qingwuit\Models\StoreClass;
use App\Qingwuit\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;

// use App\Services\AuthService;

class AuthController extends Controller
{
    // 登录
    public function login()
    {
        $rs = $this->getService('Auth')->login();
        return $rs;
//        return $this->handle($rs);
    }

    // 注册
    public function register()
    {
//        echo 1111;die;
        $rs = $this->getService('Auth')->register();
        return $this->handle($rs);
    }

    // 找回密码
    public function forget_password()
    {
        $rs = $this->getService('Auth')->forgetPassword();
        return $this->handle($rs);
    }

    // 退出账号
    public function logout()
    {
        return $this->success($this->getService('Auth')->logout());
    }
    public function phone_login(Request $request)
    {
        $phone = $request->input('phone');
        $code = $request->input('code');
        $identity = $request->input('identity');

        // 验证手机号和验证码是否为空
        if (empty($phone) || empty($code)) {
            return $this->errorfrom('401','手机号和code不能为空','');
        }

        // 验证手机号格式
        if (!preg_match("/^1[34578]\d{9}$/", $phone)) {
            return $this->errorfrom('401','手机号格式不正确','');
        }

        // 检查验证码是否正确
        $smsCheck = $this->getService('Sms')->checkSms($phone, $code,'login');
        if (!$smsCheck['status']) {
            return $this->formatError($smsCheck['msg']);
        }

        if ($smsCheck) {
            // 验证用户是否存在
            $user = User::where('phone', $phone)->first();

            if (!$user) {
                return $this->errorfrom('401','账号未注册','');
            }
            if ($user->identity != $identity) {
                return   $this->errorfrom('500','此身份未注册','');
            }
            // 登录用户
            $data = Auth::login($user);
            auth()->login($user);
            $token = auth()->user()->createToken('MyApp')->accessToken;
            $data['token_type'] = 'Bearer';
            $data['access_token'] = $token;
            return $this->success($data);
        }
// else {
//            return  $this->errorfrom(__('401','手机号和code不能为空'));
//        }
    }

    // 获取用户信息
    public function info(Request $request)
    {

        $info = $this->getUser('users');
        $user_id = $this->getUserId('users');
        $inite = User::where('id',$user_id)->first();
        $identity = $inite->identity;

        if (!$info['status'] || empty($info['data'])) return $this->error(__('tip.userThr'));
        if (isset($info['data']['password'])) unset($info['data']['password']);
        if (isset($info['data']['pay_password'])) unset($info['data']['pay_password']);
        if ($identity){
            if ($identity == 1){
                $data = Parlours::where('userid', $user_id)->first();
                $address_id = Address::where('user_id', $user_id)->first();
                if ($address_id){
                    $address_id = $address_id->id;
                }else{
                    $address_id = '';
                }
                $info['data']['avatar'] = $data->avatar;
                $info['data']['business_license'] = $data->business_license;
                $info['data']['parlour_name'] = $data->parlour_name;
                $info['data']['parlour_contacts'] = $data->parlour_contacts;
                $info['data']['parlour_phone'] = $data->parlour_phone;
                $info['data']['address'] = $data->address;
                $info['data']['address_id'] = $address_id;
            }
            if ($identity == 2){
                $data = Riders::where('user_id', $user_id)->first();
                $info['data']['picture'] = $data->picture;
                $info['data']['name'] = $data->name;
                $info['data']['phone'] = $data->phone;
                $info['data']['gender'] = $data->gender;
                $info['data']['cardid'] = $data->cardid;
                $info['data']['driving_license'] = $data->driving_license;
                $info['data']['vehicle_model'] = $data->vehicle_model;
            }
            if ($identity == 3){

                $data = Store::where('user_id', $user_id)->first();
                if ($data->isvalidate== 0){
                    $info['data']['iscompletion'] = 2;
                }else{
                    $info['data']['iscompletion'] = 1;
                }
                $arr= StoreClass::where('store_id', $data->id)->first();
                $info['data']['isclass'] = 1;
                if (empty($arr)){
                    $info['data']['isclass'] = 2;
                }

                $info['data']['store_logo'] = $data->store_logo;
                $info['data']['store_name'] = $data->store_name;
                $info['data']['store_description'] = $data->store_description;
                $info['data']['legal_person'] = $data->legal_person;
                $info['data']['store_phone'] = $data->store_phone;
                $info['data']['store_address'] = $data->store_address;
                $info['data']['store_face_image'] = $data->store_face_image;
                $info['data']['business_license'] = $data->business_license;

            }
        }

        return $this->handle($info);
    }

    // 账号编辑
    public function edit(Request $request)
    {
        $data = $request->except('provider');
        $id = $this->getUserId($request->provider);
        $pro = ucfirst(str_replace('api/', '', $request->route()->action['prefix']));
        if ($pro != 'Admin') {
            $pro = 'User';
        }
        if (!isset($data['password']) || empty($data['password'])) {
            unset($data['password']);
        }
        if (isset($data['password'])) {
            $data['password'] = Hash::make($data['password']);
        }
        if (!isset($data['pay_password']) || empty($data['pay_password'])) {
            unset($data['pay_password']);
        }
        if (isset($data['pay_password'])) {
            $data['pay_password'] = Hash::make($data['pay_password']);
        }

        // 修改手机号码
        if (isset($data['phone']) && !empty($data['phone']) && $pro == 'User') {
            if ($this->getService($pro, true)->whereNotIn('id', [$id])->where('phone', $data['phone'])->exists()) {
                return $this->error(__('tip.phoneExist'));
            }
            $sms = $this->getService('Sms')->checkSms($data['phone'], $data['code']);
            if (!$sms['status']) {
                return $this->error($sms['msg']);
            } else {
                unset($data['code']);
            }
        }

        $rs = $this->getService($pro, true)->where('id', $id)->update($data);
        return $this->success($rs);
    }

    // 验证用户是否登录
    public function is_login(Request $request)
    {
        try {
            $id = $this->getUserId($request->provider);
            return !empty($id) ? $this->success($id) : $this->auto(200);
        } catch (\Exception $e) {
            return $this->auto(200, $e->getMessage());
        }
    }

    // 验证码请求
    public function captcha()
    {
        return $this->handle($this->getService('Captcha')->createCap());
    }

}
